//
//  JQSettingViewController.m
//  JQKit
//
//  Created by JQZ on 2017/12/29.
//  Copyright © 2017年 JQZ. All rights reserved.
//

#import "JQSettingViewController.h"
#import "ShortcutMacros.h"

#import "JQSettingHeaderTitleView.h"
#import "JQSettingFooterTitleView.h"
#import "JQSettingCell.h"
#import "JQSettingSwitchCell.h"
#import "JQSettingButtonCell.h"

@interface JQSettingViewController ()

@end

@implementation JQSettingViewController

- (void)loadView
{
    self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.tableView setTableHeaderView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT_SETTING_TOP_SPACE)]];
    [self.tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT_SETTING_BOTTOM_SPACE)]];
    [self.tableView setBackgroundColor:[UIColor grayColor]];
    [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 15, 0, 0)];
    [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 0)];
    [self.tableView setSeparatorColor:[UIColor grayColor]];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self registerCellClass];
}

- (void)dealloc
{
#ifdef DEBUG_MEMERY
    NSLog(@"dealloc %@", self.navigationItem.title);
#endif
}

#pragma mark - Public -
- (void)registerCellClass
{
    [self.tableView registerClass:[JQSettingHeaderTitleView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([JQSettingHeaderTitleView class])];
    [self.tableView registerClass:[JQSettingFooterTitleView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([JQSettingFooterTitleView class])];
    [self.tableView registerClass:[JQSettingCell class] forCellReuseIdentifier:NSStringFromClass([JQSettingCell class])];
    [self.tableView registerClass:[JQSettingSwitchCell class] forCellReuseIdentifier:NSStringFromClass([JQSettingSwitchCell class])];
    [self.tableView registerClass:[JQSettingButtonCell class] forCellReuseIdentifier:NSStringFromClass([JQSettingButtonCell class])];
}

#pragma mark - Delegate -
//MARK: UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.data.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.data[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JQSettingItem *item = [self.data[indexPath.section] objectAtIndex:indexPath.row];
    
    id cell = [tableView dequeueReusableCellWithIdentifier:item.cellClassName];
    [cell setItem:item];
    
    return cell;
}

//MARK: UITableViewDelegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    JQSettingGroup *group = self.data[section];
    if (group.headerTitle == nil) {
        return nil;
    }
    
    NSString *headerViewID = NSStringFromClass([JQSettingHeaderTitleView class]);
    JQSettingHeaderTitleView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerViewID];
    [view setText:group.headerTitle];
    
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    JQSettingGroup *group = self.data[section];
    if (group.footerTitle == nil) {
        return nil;
    }
    
    NSString *footerViewID = NSStringFromClass([JQSettingFooterTitleView class]);
    JQSettingFooterTitleView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:footerViewID];
    [view setText:group.footerTitle];
    
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    JQSettingItem *item = [self.data[indexPath.section] objectAtIndex:indexPath.row];
    if (item.itemOpertion){
        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
        item.itemOpertion(selectedCell, item);
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT_SETTING_CELL;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    JQSettingGroup *group = self.data[section];
    return 0.5 + (group.headerTitle == nil ? 0 : 5.0f + group.headerHeight);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    JQSettingGroup *group = self.data[section];
    return 20.0f + (group.footerTitle == nil ? 0 : 5.0f + group.footerHeight);
}

@end
